package com.fddh.springboot.controller;

import com.fddh.springboot.dto.OptionsDto;
import com.fddh.springboot.dto.QuestionDto;
import com.fddh.springboot.dto.ResponseDto;
import com.fddh.springboot.entity.Question;
import com.fddh.springboot.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin //@CrossOrigin解决前后端跨域问题
@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    @Qualifier("QuestionServiceImpl") //取得bean，指定bean名称为QuestionServiceImpl
    private QuestionService questionService;

    /**
     * 添加题目
     */
    //addQuestion  questionDto对应用户请求携带的数据类型
    @ResponseBody
    @RequestMapping(value = "/addQuestion")
    public QuestionDto addUser(@RequestBody QuestionDto questionDto, HttpServletRequest request) {

        //用于返回给前端的数据体
        QuestionDto responseQuestionDto = new QuestionDto();

        int insertNum = 0;//插入条目数
        int questionDtoId = 0;
        //设置创建时间字段是当前系统时间
        questionDto.setRow_create_time(new Date());
        questionDto.setRow_update_time(new Date());

        System.out.println("问题请求数据是："+questionDto);

        //mapper的插入默认返回的是插入条目数
        insertNum = questionService.addQuestion(questionDto);

        //questionDto插入成功后，questionDto就获得了自增id,通过getId()直接获取，其实这是在mapper.xml实现的.
        questionDtoId = questionDto.getId();

        System.out.println("插入题目的条数："+insertNum);
        System.out.println("插入题目的ID："+questionDtoId);

        //将题号返回给前端,用responseQuestionDto携带题目ID返回
        responseQuestionDto.setId(questionDtoId);
        return responseQuestionDto;
    }

    /**
     * 查询题目列表
     * */
    @ResponseBody
    @RequestMapping(value = "/getQuestionList")
    public List<Question> getQuestionList(){

        //ResponseDto对应返回给前端的数据
        ResponseDto responseDto = new ResponseDto();

        List<Question> list = new ArrayList<>();
        list = questionService.getQuestionList();
        //将list数据放到responseDto的Content中
        //responseDto.setContent(list);
        //返回数据responseDto给前端
        System.out.println("获得的数据是："+list);
        return list;

    }

//    /**
//     * 删除题目
//     * 返回删除成功的标志flag
//     * */
//    @ResponseBody
//    @RequestMapping(value = "/addQuestion")
//    public int deleteQuestion(@RequestBody QuestionDto questionDto, HttpServletRequest request){
//
//        //ResponseDto对应返回给前端的数据
//        ResponseDto responseDto = new ResponseDto();
//
//        //返回是否删除成功的标志
//        int flag = 0;
//        flag = QuestionService.deleteQuestion(userDto);
//
//        if(flag!=0){
//            responseDto.setCode(YesNoEnum.YES);//删除成功
//        }else{
//            responseDto.setCode(YesNoEnum.NO);
//        }
//        return responseDto;
//
//    }


    /**
     * 修改题目
     *
     * */
    //getQuestion  questionDto对应用户请求携带的数据类型



    /**
     * 添加选项
     */
    //addOption  OptionDto对应用户请求携带的数据类型
    @ResponseBody
    @RequestMapping(value = "/addOptions")
    public OptionsDto addOptions(@RequestBody List<OptionsDto> optionDto, HttpServletRequest request){

        //插入数据条数
        int resultNum = 0;
        //返回给前端的标记,给OptionsDto的Text字段
        String resultText= null;

        //各个选项设置时间
        for(int i = 0;i<optionDto.size();i++){
            //设置创建时间字段是当前系统时间
            optionDto.get(i).setRow_create_time(new Date());
            optionDto.get(i).setRow_update_time(new Date());

            System.out.println("本次插入数据是："+optionDto.get(i));

            resultNum += questionService.addOptions(optionDto.get(i));

        }
        resultText = resultNum > 0 ? "SUCCESS" : "FAILED";

        //用于返回给前端的数据体responseOptionsDto
        OptionsDto responseOptionsDto = new OptionsDto();
        responseOptionsDto.setText(resultText);
        return responseOptionsDto;

    }

    /**
     * 修改题目模块，仅仅将row_create_time赋值成当前时间
     */

}
